<?php

class SidebarsResource extends Resource
{
    const ID = "sidebars";

    public function GetId()
    {
        return self::ID;
    }

    public function GetFreshData()
    {
        $xml = @file_get_contents( SIDEBARS_FILE );
        $doc = Util::CreateXmlDocument( $xml );
        if ( $doc )
        {
            $xpath = new XPathHelper($doc);

            $panels = $this->ParsePanels( $xpath );
            $sidebars = $this->ParseSidebars( $xpath );

            $languagesSidebars = $this->LoadLngPagesSidebars();
            $sidebars = array_merge( $sidebars, $languagesSidebars );

            return array(
                'sidebars' => $sidebars,
                'panels'   => $panels
            );
        }
        return null;
    }

    private function ParsePanels( $xpath )
    {
        $panels = array();
        $nodes = $xpath->GetNodes("/sidebars/panels/panel");
        foreach ( $nodes as $node )
        {
            $panelInfo = array(
                'id'        => $node->getAttribute('id'),
                'template'  => $node->getAttribute('template'),
                'className' => $node->getAttribute('class')
            );
            $panels[$panelInfo['id']] = $panelInfo;
        }

        return $panels;
    }

    private function ParseSidebars( $xpath )
    {
        $sidebars = array();
        $nodes = $xpath->GetNodes("/sidebars/sidebar");
        foreach ( $nodes as $node )
        {
            $sidebarPanels= array();
            $panelNodes = $xpath->GetNodes("panel", $node);
            foreach ( $panelNodes as $panelNode )
            {
                $sidebarPanels[] = $panelNode->getAttribute('id');
            }

            $sidebars[ $node->getAttribute('page_id') ] = $sidebarPanels;
        }

        return $sidebars;
    }

    private function LoadLngPagesSidebars()
    {
        $sidebars = array();

        $languages = SiteLanguage::GetAdditionalLanguagesList();
        if ( !empty( $languages ) )
        {
            foreach ( $languages as $lng )
            {
                $filePath = str_replace( '%LNG%', $lng, LNG_SIDEBARS_FILE_PATTERN );
                if ( !file_exists( $filePath ) )
                    continue;

                $xml = @file_get_contents( $filePath );
                $doc = Util::CreateXmlDocument( $xml );
                if ( $doc )
                {
                    $xpath = new XPathHelper($doc);

                    $lngSidebars = $this->ParseSidebars( $xpath );
                    if ( !empty( $lngSidebars ) )
                    {
                        foreach ( $lngSidebars as $pageId => $panels )
                        {
                            $lngPageId = PageInfo::GetTranslatedPageId( $pageId, $lng );
                            $sidebars[$lngPageId] = $panels;
                        }
                    }
                }
            }
        }

        return $sidebars;
    }
}

?>